#!/bin/bash
#########################################################################
#
# File:         ndash
# Description:  create dashboard of Kibana for nagios performance data.
# Language:     GNU Bourne-Again SHell
# Version:      1.1.1
# Date:         2017-12-1
# Corp.:        liaoyichang
# Author:       atdmliaod@tom.com
#########################################################################

SOURCE="$( cd "$( dirname "$0"  )" && pwd  )"
JSON_CMD=@tools@/jsonread
ES_CMD=@tools@/minies
ES_SERVER=@es_server@

ES_PORT=9200
IDC="@idc_name@"
service_name=""
host=""
mode="set"

print_help_msg(){
        print_version
        $ECHO "Usage: $0 -h to get help."
        $ECHO 
        $ECHO 'Report bugs to: liaoyq@tom.com'
}
print_full_help_msg(){
        print_version
        $ECHO "Usage:"
        $ECHO "$0  [ -h ] -n host name"
}
print_version(){
        $ECHO $(cat $0 | head -n 7 | tail -n 1|sed 's/\# //')
}

to_debug(){
                $ECHO "`date +'%Y-%m-%d:%H:%M:%S'`|$*"
}
to_debugn(){
                $ECHO -n "$*"
}
case "$(uname -s)"
        in
        SunOS)
        ECHO="echo"
        ;;
        Linux)
        ECHO="echo -e"
        ;;
        *)
        ECHO="echo"
        ;;
esac
if [ ! -x "$JSON_CMD" ];then
        $ECHO "Can not found command $JSON_CMD, pleas check it"
        exit 3
fi
if [ ! -x "$ES_CMD" ];then
        $ECHO "Can not found command $ES_CMD, pleas check it"
        exit 3
fi

	while getopts :hn:l OPTION
	do
		case $OPTION
			in
      n)
        host=$OPTARG
      ;;
      l)
        mode="list"
      ;;
			h)
			print_full_help_msg
			exit 3
			;;			
			?)
			$ECHO "Error: Illegal Option."
			print_help_msg
			exit 3
			;;
		esac
	done		
sdate=`date +'%Y-%m'`
stime=`date +'%Y-%m-%dT%H:%M:%S'`
if [ "$host""ZZZ" == "ZZZ" ]; then
    $ECHO "Error: missing  parameter host"
    print_full_help_msg
    exit 3
fi
HOLD1=/tmp/ngraph.$$

$ES_CMD -s $ES_SERVER -m GET -u "/.kibana/dashboard/_search?q=title:\"$IDC-$host\"&pretty" > $HOLD1
if [ "$mode" == "list" ]; then
    cat $HOLD1
    $JSON_CMD -f _id $HOLD1    
    rm -rf $HOLD1
    exit 0
fi

$ECHO "setup dashboard for $service_name of $host"

dash_id=`$JSON_CMD -f _id $HOLD1|awk -F'"' '{print $4}'`
if [ "$dash_id""ZZZ" != "ZZZ" ]; then
    $ECHO "Found dashboard id=$dash_id"
fi

graph=`$ES_CMD -s $ES_SERVER -m GET -u "/.kibana/visualization/_search?q=title:\"$IDC-$host-\"&pretty"|grep "_id"|awk -F'"' '{ print $4}'`
$ECHO "Foud visualization:"
$ECHO "$graph"
if [ "$graph_id""ZZZ" != "ZZZ" ]; then
    rm -rf $HOLD1
    exit 1
fi

$ECHO '{' > $HOLD1
$ECHO "    \"title\":\"$IDC-$host\"," >> $HOLD1
$ECHO "    \"hits\" : 0, \"description\" : \"$IDC-$host\"," >> $HOLD1
$ECHO -n '    "panelsJSON" : "[' >> $HOLD1
index=1
a=0
tmp=""
row=1
for i in $graph; do
    if [ "$a" == "0" ]; then 
    tmp=$tmp"{\\\"size_x\\\":12,\\\"size_y\\\":3,\\\"panelIndex\\\":$index,\\\"type\\\":\\\"visualization\\\",\\\"id\\\":\\\"$i\\\",\\\"col\\\":1,\\\"row\\\":$row},"
       a=1
       let row++
    else
    #tmp=$tmp"{\\\"size_x\\\":6,\\\"size_y\\\":3,\\\"panelIndex\\\":$index,\\\"type\\\":\\\"visualization\\\",\\\"id\\\":\\\"$i\\\",\\\"col\\\":7,\\\"row\\\":$row},"
    tmp=$tmp"{\\\"size_x\\\":12,\\\"size_y\\\":3,\\\"panelIndex\\\":$index,\\\"type\\\":\\\"visualization\\\",\\\"id\\\":\\\"$i\\\",\\\"col\\\":1,\\\"row\\\":$row},"
       a=0
       let row++
    fi
    let index++
done
tmp=${tmp%?}
$ECHO "$tmp]\"," >> $HOLD1
$ECHO '    "optionsJSON" : "{\"darkTheme\":false}",' >> $HOLD1
$ECHO '    "uiStateJSON" : "{}","version" : 1,"timeRestore" : false,' >> $HOLD1
$ECHO '    "kibanaSavedObjectMeta" : { "searchSourceJSON" : "{\"filter\":[{\"query\":{\"match_all\":{}}}],\"highlightAll\":true,\"version\":true}" }' >> $HOLD1
$ECHO '}' >> $HOLD1

if [ "$dash_id""ZZZ" == "ZZZ" ]; then
    minies -v 4 -s $ES_SERVER -m POST -d $HOLD1 -u "/.kibana/dashboard/"
else
    minies -v 4 -s $ES_SERVER -m PUT -d $HOLD1 -u "/.kibana/dashboard/$dash_id"
fi
rm -rf $HOLD1
